package com.gexiaobao.pigeon.ui.fragment.mine.pigeon

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.BobInfoResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentAutotrophicBinding
import com.gexiaobao.pigeon.ui.adapter.FosterPigeonAdapter
import com.gexiaobao.pigeon.ui.fragment.mine.pigeon.activity.ActivityShedAddPigeon
import com.gexiaobao.pigeon.viewmodel.MyPigeonViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity
import org.angmarch.views.OnSpinnerItemSelectedListener

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 17:20
 * @Describe    : 我的赛鸽-公棚
 */
class FragmentFoster : BaseFragment<MyPigeonViewModel, FragmentAutotrophicBinding>() {

    private val arrayList: ArrayList<String> = arrayListOf()
    private val mAdapter: FosterPigeonAdapter by lazy { FosterPigeonAdapter(arrayListOf()) }
    private var mOrgId: String = ""
    private var dataResult: ArrayList<BobInfoResponse.ListData> = arrayListOf()

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mViewModel.pigeonNum.set(getString(R.string.pigeon_number_zero))
        mViewModel.pigeonType.set("2")
        mViewModel.autoPigeon.set(false)
        mViewModel.fosterPigeon.set(true)
        mDatabind.tvAutoAddPigeon.isVisible = true

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 1f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getLoftOrgListByUserWithOrg(false, mOrgId, "", "")
            }
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getLoftOrgListByUserWithOrg(true, mOrgId, "", "")
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getLoftOrgListByUserWithOrg(true, mOrgId, "", "")
        }
        mAdapter.setOnItemClickListener { _, _, position ->
//            startActivity<ActivityShedPigeonDetail>("pigeonId" to mAdapter.data[position].id.toString(), "pigeonData" to mAdapter.data[position])
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvAutoAddPigeon) {
            when (it) {
                mDatabind.tvAutoAddPigeon -> {
                    startActivity<ActivityShedAddPigeon>("type" to 1)
                }
            }
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.spinnerGp.hint = getString(R.string.click_and_choose_shed)
        mDatabind.spinnerGp.onSpinnerItemSelectedListener = OnSpinnerItemSelectedListener { parent, view, position, id ->
            mOrgId = if (position == 0) "" else dataResult[position - 1].id.toString()
            loadsir.showLoading()
            mViewModel.getLoftOrgListByUserWithOrg(true, mOrgId, "", "")
        }

        eventViewModel.toRefreshList.observeInFragment(this) {
            if (it) {
                loadsir.showLoading()
                mViewModel.getLoftOrgListByUserWithOrg(true, mOrgId, "", "")
            }
        }
        eventViewModel.toRefreshData.observeInFragment(this) {
            mViewModel.getLoftOrgListByUserWithOrg(true, mOrgId, "", "")
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.getLoftOrgListByUser()
        mViewModel.getLoftOrgListByUserWithOrg(true, mOrgId, "", "")
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.fosterPigeonData.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
                mViewModel.pigeonNum.set(getString(R.string.pigeon_numbers) + ":" + it.total + getString(R.string.yu))
            } else {
                RxToast.showToast(it.errMessage)
            }
        }

        mViewModel.BobInfoResult.observe(viewLifecycleOwner) {
            if (!it.isEmpty()) {
                dataResult.addAll(it.list)
                arrayList.clear()
                arrayList.add(0, getString(R.string.activity_mine_account_transaction_type1_all))
                it.list.forEachIndexed { _, listData ->
                    arrayList.add(listData.name)
                }
                mDatabind.spinnerGp.attachDataSource(arrayList)
            }
        }
    }
}